<?php
/**
 * 抽奖小程序我的中心相关接口
 * 作者：刘单风
 * 最后修改时间：2018-11-14
 * 版权：医库PHP小组
 */
namespace app\api\controller;
use MoCommon\DataState\AddressCodes;
use MoCommon\Support\Codes;
use app\model\Address;
class Luckaddress extends Apibase
{

    private static $addressModel;
    public function __construct()
    {
        self::$addressModel=new Address();
    }
    /**
     * 收货地址列表
     */
    function receiveaddress()
    {
        $userid = parent::getUid();
        $where = [
            'user_id' => $userid
        ];
        $data = self::$addressModel->dataList($where);
        return parent::respond($data);
    }

    /**
     * 保存收货地址
     */
    function saveaddress()
    {
        $userid = parent::getUid();
        $consignee = input('consignee');
        $link_phone = input('link_phone');
        $link_area = input('link_area');
        $link_address = input('link_address');
        $is_default = input('is_default');
        $id = input('id');
        if (empty($consignee) || empty($link_phone) || empty($link_address)) {
            //参数不全
            $status = Codes::PARAM_ERR;
            $msg = Codes::get($status);
        } else {
            $data = [
                'consignee' => $consignee,
                'link_phone' => $link_phone,
                'link_area' => $link_area,
                'link_address' => $link_address,
                'is_default' => $is_default,
                'user_id' => $userid
            ];
            $where = $data;
            if ($id) {
                $where['id'] = "!=" . $id;
            }
            $isdata = self::$addressModel->isExist($where);
            if (empty($isdata)) {
                if ($is_default == 1) {
                    //说明当前的为默认地址,将其他地址取消默认
                    self::$addressModel->updateOne(['is_default' => 0], ['user_id' => $userid]);
                }
                if ($id) {
                    $addrid = $id;
                    //更新收货地址
                    self::$addressModel->updateOne($data, ['id' => $id]);
                } else {
                    //新增收货地址
                    $addrid = self::$addressModel->addOne($data);
                }
                $status = Codes::ACTION_SUC;
                $msg = Codes::get($status);
            } else {
                //收货地址已存在
                $status = AddressCodes::DATA_EXIST;
                $msg = AddressCodes::get($status);
            }
        }

        return parent::respond($addrid, $status, $msg);
    }

    /**
     * 获取地址详情信息
     */
    function addressdetail()
    {
        $userid = parent::getUid();
        $id = input('id');
        $data = self::$addressModel->oneDetail(['id' => $id, 'user_id' => $userid]);
        return parent::respond($data);
    }

    /**
     * 删除收货地址
     */
    function deladdress()
    {
        $userid = parent::getUid();
        $id = input('id');
        if (empty($id)) {
            //参数不全
            $status = Codes::PARAM_ERR;
            $msg = Codes::get($status);
        } else {
            self::$addressModel->updateOne(['is_logicdel' => 1], ['id' => $id, 'user_id' => $userid]);
            $status = Codes::ACTION_SUC;
            $msg = Codes::get($status);
        }
        return parent::respond([], $status, $msg);
    }


    /**
     * 设置默认地址
     */
    function setdefault()
    {
        $userid = parent::getUid();
        $id = input('id');
        $datatype = input('datatype');//0取消默认;1设置默认
        if ($id) {
            //参数不全
            $status = Codes::PARAM_ERR;
            $msg = Codes::get($status);
        } else {
            $data = ['is_default' => $datatype];
            $where = ['id' => $id, 'user_id' => $userid];
            self::$addressModel->updateOne($data, $where);
            $status = Codes::ACTION_SUC;
            $msg = Codes::get($status);
        }
        return parent::respond([], $status, $msg);
    }
}